﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post.Service;
using WuDada.Core.SystemApplications.Domain;
using WuDada.Core.Auth.Domain;

public partial class admin_UC02_0206_2 : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private IAttachedFileService m_AttachedFileService;
    private WebLogService m_WebLogService;

    //傳入的父層Id
    private int m_ParentPostId
    {
        get { if (ViewState["parent"] == null) { ViewState["parent"] = 0; } return int.Parse(ViewState["parent"].ToString()); }
        set { ViewState["parent"] = value; }
    }
    private string m_ParentPath = "0206.aspx";

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }
    private string m_PicFileName
    {
        get { if (ViewState["picfilename"] == null) { ViewState["picfilename"] = string.Empty; } return ViewState["picfilename"].ToString(); }
        set { ViewState["picfilename"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_WebLogService = new WebLogService();
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();
        m_AttachedFileService = m_PostFactory.GetAttachedFileService();

        if (!IsPostBack)
        {
            pnlContent.Visible = false;
            m_ParentPostId = QueryStringHelper.GetInteger("parent", 0);
            PostVO parentPost = m_PostService.GetPostById(m_ParentPostId);
            if (parentPost == null)
            {
                string strJs = JavascriptUtil.AlertJSAndRedirect("查無父層分類資訊", m_ParentPath);
                ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", strJs, false);
            }

            lblParentTitle.Text = parentPost.Title;
            fillGridView();
            ShowMode();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnAdd.Visible = true;
            btnSave.Visible = false;
        }
        else
        {
            btnAdd.Visible = false;
            btnSave.Visible = true;
        }
    }

    private void fillGridView()
    {
        //搜尋條件
        DateTime? startDate = null;
        string sortField = "SortNo";
        bool sortDesc = false;

        //分頁
        AspNetPager1.RecordCount = m_AttachedFileService.CountAttachedFileListByPostId(
            m_ParentPostId, false, AttachedFileVO.Type.Video, startDate);
        int pageIndex = (AspNetPager1.CurrentPageIndex - 1);
        int pageSize = AspNetPager1.PageSize;

        gvList.DataSource = m_AttachedFileService.GetAttachedFileListByPostId(
            m_ParentPostId, false, AttachedFileVO.Type.Video, startDate, pageIndex, pageSize, sortField, sortDesc);
        gvList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AttachedFileVO attachedFileVO = new AttachedFileVO();
        attachedFileVO.FileType = AttachedFileVO.Type.Video;
        attachedFileVO.Title = txtTitle.Text.Trim();
        attachedFileVO.SortNo = int.Parse(txtSortNo.Text.Trim());
        attachedFileVO.Post = m_PostService.GetPostById(m_ParentPostId);
        attachedFileVO.Content = txtContent.Text.Trim();
        attachedFileVO.FileName = m_PicFileName;
        attachedFileVO.Flag = int.Parse(ddlFlag.SelectedValue);
        m_WebLogService.FillAdminOprtAction(attachedFileVO, FuncOprt.OprtAction.INSERT);
        m_AttachedFileService.CreateAttachedFile(attachedFileVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.新增, attachedFileVO);
        ClearUI();
        fillGridView();
    }

    private void ClearUI()
    {
        m_Mode = 0;
        m_PicFileName = string.Empty;
        ltlImg.Text = string.Empty;
        txtTitle.Text = string.Empty;
        txtSortNo.Text = string.Empty;
        txtContent.Text = string.Empty;
        ddlFlag.SelectedValue = string.Empty;
        pnlContent.Visible = false;
        btnShowAdd.Enabled = true;
    }

    private string GetPic(string fileName)
    {
        return "<img src='../../upload/" + fileName + "' width='145' height='108' border='0'>";
    }

    protected void gvList_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int fileId = int.Parse(e.CommandArgument.ToString());
        AttachedFileVO attachedFileVO = m_AttachedFileService.GetAttachedFileById(fileId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                m_Mode = fileId;
                m_PicFileName = attachedFileVO.FileName;
                ltlImg.Text = GetPic(m_PicFileName);
                txtTitle.Text = attachedFileVO.Title;
                txtSortNo.Text = attachedFileVO.SortNo.ToString();
                txtContent.Text = attachedFileVO.Content;
                ddlFlag.SelectedValue = attachedFileVO.Flag.ToString();
                ShowMode();
                pnlContent.Visible = true;
                break;
            case "myDel":
                m_AttachedFileService.DeleteAttachedFile(attachedFileVO);
                m_WebLogService.AddSystemLog(MsgVO.Action.刪除, attachedFileVO);
                break;            
            default:
                break;
        }
        fillGridView();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            AttachedFileVO attachedFileVO = m_AttachedFileService.GetAttachedFileById(m_Mode);
            attachedFileVO.Title = txtTitle.Text.Trim();
            attachedFileVO.SortNo = int.Parse(txtSortNo.Text.Trim());
            attachedFileVO.Content = txtContent.Text.Trim();
            attachedFileVO.FileName = m_PicFileName;
            attachedFileVO.Flag = int.Parse(ddlFlag.SelectedValue);            
            m_WebLogService.FillAdminOprtAction(attachedFileVO, FuncOprt.OprtAction.UPDATE);
            m_AttachedFileService.UpdateAttachedFile(attachedFileVO);
            m_WebLogService.AddSystemLog(MsgVO.Action.修改, attachedFileVO);
            fillGridView();
            ClearUI();
            ShowMode();
        }
        catch (Exception ex)
        {
            m_Log.Error(ex);
            lblMsg.Text = ex.ToString();
        }
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowIndex != -1)
        //{
        //    Control ctrl = e.Row;
        //    int attachedFileId = int.Parse(UIHelper.FindHiddenValue(ref ctrl, "hdnAttachedFileId"));
        //    AttachedFileVO attachedFileVO = m_AttachedFileService.GetAttachedFileById(attachedFileId);            
        //}
    }

    protected void btnShowAdd_Click(object sender, EventArgs e)
    {
        ClearUI();
        m_PicFileName = string.Empty;
        ShowMode();
        pnlContent.Visible = true;
        btnShowAdd.Enabled = false;
    }

    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void btnUpliad_Click(object sender, EventArgs e)
    {
        try
        {
            HttpFileCollection hfc = Request.Files;
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    string fileName = System.IO.Path.GetFileName(hpf.FileName);
                    hpf.SaveAs(Server.MapPath("~\\") + "\\upload\\" + fileName);
                    ltlImg.Text = GetPic(fileName);
                    m_PicFileName = fileName;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("請點選您要上傳的照片!"), false);
                    return;
                }
            }
        }
        catch
        {
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "js", JavascriptUtil.AlertJS("檔案傳輸錯誤!"), false);
            return;
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(m_ParentPath);
    }
}